Destroys an instance - at the end of the current frame. The instance finishes the frame first (the rest of its events run, and other instances still find it); then its Destroy event runs, once, and it is removed.
instance_destroy(id)| Name | Type |
|---|---|
id | ref<object> |
Nothing.
Ends one instance - but not right away. The instance finishes the frame it was destroyed in. The rest of the event that called this keeps running, and so does everything else that frame still has for that instance: its Step End, its Draw, and every other event that comes later. Other instances can still see it too. Only when the frame is over does its Destroy event run, once, and then the instance is removed.
So instance_destroy(self); is not a stop sign for the code around it. Lines after it run, and they see everything just as it was before the call, because Destroy has not run yet:
if (y > 500) {
instance_destroy(self);
speed = 0; // still runs, in this same frame
}Calling this a second time on an instance that was already destroyed this frame does nothing, and its Destroy event never runs twice, so there is no need to guard every call with an instance_exists check first.
The one thing a destroyed instance stops doing is hitting things. For the rest of that frame its Collision events do not run, and it does not take part in anyone else's (see Collision).
Destroying is also what actually releases what an instance was holding - its sprite palette bank, its sprite tile set, its affine matrix if it had one. That happens at the end of the frame, with the Destroy event. Hiding it instead (visible = false) does not: a hidden instance still exists, so it still counts. See Budgets for why "not currently drawn" and "not costing anything" are different questions on this hardware.
if (y > 500) {
instance_destroy(self);
}Destroys this instance once it drifts past y = 500 (off the bottom of a typical room) - a simple, self-contained despawn condition.




An instance that lives to the end of the frame has a second consequence, and this one catches people out: for the rest of that frame, every function that finds instances still finds the ones you destroyed. instance_exists says yes, instance_count has not gone down, and instance_nearest, collision_rect and collision_point all still hand them back. In the next frame it is gone for all of them.
So the loop that looks obvious never ends:
// Do not write this. It hangs.
target = instance_nearest(x, y, obj_enemy);
while (target != nothing) {
instance_destroy(target);
target = instance_nearest(x, y, obj_enemy); // finds the same one, forever
}There is nothing to fix inside that loop - instance_nearest is behaving correctly, and on a handheld the only symptom is a game that stops. The compiler warns about this shape when it can see it.
Turn the question around: instead of asking the world "what is in this box?" over and over, visit each instance once and let it decide.
// Every enemy touching this instance's own mask.
with (obj_enemy) {
if (collides(other)) {
instance_destroy(self);
}
}
// Or an arbitrary box, with the same shape.
with (obj_enemy) {
if (x > other.x - 40 && x < other.x + 40 && y > other.y - 40 && y < other.y + 40) {
instance_destroy(self);
}
}Inside the with, self is the enemy being visited and other is whatever ran the block - the blast, the trap, the player. Each instance is reached exactly once whether or not it gets destroyed, so this always finishes, and it is the only shape that can reach every match rather than just the first one collision_rect happens to return.
When the frame is over, every instance destroyed in it gets its Destroy event, one after another, and is then removed, which runs its Cleanup event too. The frame has already been shown by then, so the instance was in its last picture. If several instances were destroyed in the same frame, their Destroy events run in the order your project lists the objects, and in the order they were made within one object. Do not rely on that order between different instances.
The instance is only MARKED by this call: it keeps running the rest of the frame (the code after the call, its Step End, its Draw, every other instance that looks at it), and when the frame has been shown the end-of-frame sweep runs its Destroy event, once, then Cleanup, then frees the slot. A Destroy event that destroys other instances is finished in the same sweep (the sweep goes round again until nothing is left marked). Because the slot survives until the end of the frame, a loop over live instances that destroys some of them never skips or re-visits one mid-loop. THE OTHER HALF OF THAT RULE, and the one that bites: until that end-of-frame moment a destroyed instance is still FOUND - by instance_nearest, instance_exists, instance_count, collision_rect/collision_point and the pos_/point_ tests alike. So "find one, destroy it, look for the next one" never runs out of matches and never ends; on this machine that is simply a ROM that stops. To clear out everything matching some test, sweep from the instance side instead: `with (obj_enemy) { if (collides(other)) { instance_destroy(self); } }` visits every instance exactly once and always ends - and works for an arbitrary box just as well (`if (x > other.x - 40 && x < other.x + 40 && ...)`). The compiler warns when it sees the looping shape. Calling this more than once on the same instance in the same frame is safe (a harmless no-op the second time).